<?
class ListTouristAttractionForm extends Form
{
	function ListTouristAttractionForm()
	{
		Form::Form('ListTouristAttractionForm');
		
        $this->link_css("style/paging.css");
        $this->link_css('style/jquery-ui-1.9.2.custom.css');
        $this->link_js('javascript/jquery/jquery-ui-1.9.2.custom.min.js');
        //
        $this->link_js("javascript/jquery/jquery.ui.core.js");
        $this->link_js("javascript/jquery/jquery.ui.widget.js");
        $this->link_js("javascript/jquery/jquery.ui.mouse.js");
        $this->link_js("javascript/jquery/jquery.ui.sortable.js");
        $this->link_js("javascript/tiny_mce/jquery.tinymce.js");
        $this->link_js("javascript/tiny_mce/tiny_mce.js");
        
        
        //$this->link_css('style/jquery-ui-1.9.2.custom.css');
        $this->link_css('style/msgBoxLight.css');
        //$this->link_js('javascript/jquery/jquery-ui-1.9.2.custom.min.js');
        $this->link_js("javascript/jquery/jquery.msgBox.js");
        $this->link_js("javascript/hotelform.js");

        $this->link_outside_js("http://maps.google.com/maps/api/js?sensor=false");
        $this->link_js("javascript/map/geocoder_tool.js");
        $this->link_css("style/map/geocoder_tool.css");
	}
	
	function on_submit()
	{
            
	}
	
	function draw()
	{
		global $display;
                
                $id = Url::get("id");
                $cmd = Url::get('cmd');
                $mess = '';
                if($cmd == 'del' && $id !=''){
                $tuas ["user_del"] = User::id();
                $tuas ["user_del_email"] = User::user_email();
                $tuas ["time_del"] = TIME_NOW;
                $tuas ["status"] = '-1';
                DB::update('tourist_attractions',$tuas,'id='.$id);
                $mess = 'Đã xóa thành công !';
                $display->add('mess',$mess);
                }  
                
                
                
                $con_arr = array(); // Mang dieu kien filter
                
                $tua_status = Url::getInt("tua_status",10);
        $display->add('tua_status', $tua_status);
        if($tua_status!==0){
            if($tua_status==10){
                $con_arr[] = "(status>0)";
            }else{
                $con_arr[] = "(status=$tua_status)";
            }
        }else {$con_arr[] = "(status<5)";}
        
        //Lọc theo Id tua
        $tua_id = Url::get("tua_id",'');
        $display->add('tua_id', $tua_id);
        if($tua_id){
            $con_arr[] = '(id='.(int)$tua_id.')';
        }
        //loc theo ten tin
        
        $tua_name = Url::get("tua_name",'');
        $display->add('tua_name', $tua_name);
        if($tua_name){
            $con_arr[] = '(`name` LIKE "%'.DB::escape($tua_name).'%")';
        }
        
        //loc theo mot ta
        
        $tua_intro = Url::get("tua_intro",'');
        $display->add('tua_intro',$tua_intro);
        if($tua_intro){
            $con_arr[] = '(`intro` LIKE "%'.DB::escape($tua_intro).'%")';
        }
        //loc theo noi dung
        
        $tua_content = Url::get("tua_content",'');
        $display->add('tua_content',$tua_content);
        if($tua_content){
            $con_arr[] = '(`content` LIKE "%'.DB::escape($tua_content).'%")';
        }
        // Loc theo id nguoi tao
        $tua_user_c = Url::get("tua_user_c",'');
        $display->add('tua_user_c', $tua_user_c);
        if($tua_user_c){
            $con_arr[] = '(user_c='.(int)$tua_user_c.')';
        }
        
        //Loc theo vi tri
        
        $tua_city_id = Url::get("tua_city_id",'');
        $display->add('tua_city_id', $tua_city_id);
        if($tua_city_id == 0)
        {}
        else
        if($tua_city_id){
            $con_arr[] = '(city_id='.(int)$tua_city_id.')';
        }
        // Tim theo ngay dang
        $date = Url::get("tua_time_c");
        if($date){
        $date_c = explode('/',$date);
        $date_c_tua = new DateTime(''.$date_c[2].'-'.$date_c[1].'-'.$date_c[0].' 00:00:00');
        $date_c_end = (int)$date_c_tua->getTimestamp() + (1 * 24 * 60 * 60);
        $display->add('time_c', $date);
            $con_arr[] =  '(time_c >= '.$date_c_tua->getTimestamp().')';
            $con_arr[] =  '(time_c <= '.$date_c_end.')';
        }
        
        $condition = implode($con_arr,' AND ');
        // get all dia diem in db with $condition, order by time created
        $order = ' ORDER BY time_c DESC';
        $item_per_page = 5;
        $page_no = (Url::getInt('page_no')<=1)?0:(Url::getInt('page_no')-1);
        $offset = $page_no*$item_per_page ;
        $limits = ' LIMIT '.$offset.','.$item_per_page.'';
        $condition_t = $condition; 
        
        $condition .= $order.$limits;
        $all_tuas =  DB::select_all("tourist_attractions",$condition);
        $total_tuas = count(DB::select_all("tourist_attractions",$condition_t));
        
        $display->add('paging', ECPaging::paging($limit, $total_tuas, $item_per_page));
        $display->add('total_tuas',$total_tuas);
        Db_Location::get_cities();
        $city = CGlobal::$all_cities;
        
        $this->beginForm(false,"post",false,Url::build_current());
        $location = Db_Location::getLocationByParentId(0);
        $display->add('province',$location);
        $display->add('city',$city);
                $display->add('all_tuas',$all_tuas);
                $display->add('server_img',IMAGE_PATH);
                $display->add('back', $_SERVER['REQUEST_URI']); 
//        if(User::have_permit('Categories_admin')){
//                $cate_admin = array(
//                                        'tittle' => 'Quản lý danh mục tin',
//                                        'url'   => WEB_DIR.ECRewrite::formatUrl('?page=categories')
//                );
//                $display->add('cate_admin',$cate_admin);
//        }
		$display->output('ListTouristAttraction');
		$this->endForm();
	}
}
?>